package com.jblog.common.util;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public final class MonthUtil {
    
    public enum Format {
        SHORT,
        LONG
    }

    private MonthUtil() {
    }

    private static final List<String> MONTH_LIST = Arrays.asList("january", "february", "march",
            "april", "may", "june", "july", "august", "september", "october",
            "november", "december");
    private static final Map<String, Integer> MONTH_MAP = new HashMap<String, Integer>(12);
    private static final Map<Integer, String> NUM_MAP = new HashMap<Integer, String>(12);

    static {
        for (int i = 0, len = MONTH_LIST.size(); i < len; i++) {
            MONTH_MAP.put(MONTH_LIST.get(i), i);
            NUM_MAP.put(i, MONTH_LIST.get(i));
        }
    }
    
    public static String num2Month(int num, Format format) {
        //month start at 0, so should -1
        num = num - 1;
        if (format == Format.LONG) {
            return NUM_MAP.get(num);
        } else if (format == Format.SHORT)  {
            return NUM_MAP.get(num).substring(0, 3);
        } else {
            return "ERROR"; 
        }
    }
    public static String num2Month(String num, Format format) {
        int n = 0;
        try {
            n = Integer.parseInt(num);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return num2Month(n, format);
    }

    public static Integer Month2Num(String month) {
        return MONTH_MAP.get(month);
    }
    
//    public static void main(String[] args) {
//        System.out.println(ArchiveUtil.num2Month(8));
//        System.out.println(ArchiveUtil.num2Month("09"));
//        System.out.println(ArchiveUtil.Month2Num("january"));
//    }

}
